/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.cli;

import cz.mzk.oai.metadataRepository.repository.Identifier;
import cz.mzk.oai.metadataRepository.repository.Repository;
import cz.mzk.oai.metadataRepository.repository.collections.MetadataCollection;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class MoveItemsAction extends Action {

    private static String FROM = "FROM_COLLECTION:";
    private static String TO = "TO_COLLECTION:";
    private final List<Identifier> idList = new ArrayList<Identifier>();
    private final String itemIdsFile;
    private String srcCollectionName;
    private String destCollectionName;
    

    public MoveItemsAction(Repository repository, String itemIdsFile) {
        super(repository);
        this.itemIdsFile = itemIdsFile;
    }

    @Override
    public void run() throws Exception {
        File idsFile = new File(itemIdsFile);
        if (!idsFile.exists() || idsFile.isDirectory() || !idsFile.canRead()) {
            throw new Exception("File '" + itemIdsFile + "' doesn't exist, is directory or cannot be read");
        }
        readFile(idsFile);
        MetadataCollection from = repository.findCollection(srcCollectionName);
        if (from == null) {
            throw new Exception("No such collection '" + srcCollectionName + "'");
        }
        MetadataCollection to = repository.findCollection(destCollectionName);
        if (to == null) {
            throw new Exception("No such collection '" + destCollectionName + "'");
        }
        System.out.println(from.getName() + " -> " + to.getName());
        for (Identifier id : idList) {
            System.out.println("moving " + id.toString());
            repository.moveItemBetweenCollections(id, from, to);
        }
    }

    private void readFile(File idsFile) throws Exception {
        LineNumberReader reader = new LineNumberReader(new BufferedReader(new FileReader(idsFile)));
        while (reader.ready()) {
            String line = reader.readLine();
            switch (reader.getLineNumber()) {
                case 1:
                    parseSrcCollName(line);
                    break;
                case 2:
                    parseDestCollName(line);
                    break;
                default:
                    parseId(line);
                    break;
            }
        }
    }

    private void parseSrcCollName(String line) throws Exception {
        if (line.startsWith(FROM)){
            srcCollectionName = line.substring(FROM.length());
        }else{
            throw new Exception("first line doesn't contain '" + FROM + "...");
        }
    }

    private void parseDestCollName(String line) throws Exception {
        if (line.startsWith(TO)){
            destCollectionName = line.substring(TO.length());
        }else{
            throw new Exception("second line doesn't contain '" + TO + "...");
        }
    }

    private void parseId(String line) {
        try {
            Identifier id = Identifier.parseNotEncodedString(line);
            idList.add(id);
        } catch (RuntimeException e) {
            System.err.println("cannot parse id '" + line + "': " + e.getMessage());
        }
    }
}
